﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using System.Text;
using Telerik.WebControls;
using Runda.CMS.Common.Utility;

namespace Runda.CMS.Portal.Workspace
{
    public partial class AssignUserToComment :BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<SysRoleEntity> roles = new SystemBll().GetAll<SysRoleEntity>("");
                ddlRoles.DataSource = roles;
                ddlRoles.DataTextField = "RoleName";
                ddlRoles.DataValueField = "RoleID";
                ddlRoles.DataBind();
                ddlRoles.Items.Insert(0, new ListItem("全部", ""));

                QueryUsers();
            }
        }

        private void QueryUsers()
        {
            StringBuilder sb = new StringBuilder();
            if (txtFilter.Text.Trim() != string.Empty)
            {
                sb.Append(" and (Account like '%").Append(txtFilter.Text.Trim()).Append("%'").Append(" or UserName like '%").Append(txtFilter.Text.Trim()).Append("%')");
            }

            if (ddlRoles.SelectedValue != "")
            {
                sb.Append(" and (UserID in (select UserID from Cms_Sys_UserRoleRelation where RoleID = '" + ddlRoles.SelectedValue + "'))");
            }
            List<SysUserEntity> list = new SysUserBll().GetAll<SysUserEntity>(sb.ToString());

            gridUserList.DataSource = list;
            gridUserList.DataBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            QueryUsers();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            WflInstanceRoutingEntity TrackEntity = new WflInstanceRoutingBll().Get<WflInstanceRoutingEntity>(new Guid(Request.QueryString["TrackID"]));
            if (TrackEntity == null) return;

            WflCommentBll bll = new WflCommentBll();
            List<WflCommentEntity> Comments = new List<WflCommentEntity>();
            WflCommentEntity entity = null;
            foreach (GridDataItem item in gridUserList.Items)
            {
                CheckBox cbxSelect = item.FindControl("cbxSelect") as CheckBox;
                HiddenField txtUserName = item.FindControl("txtUserName") as HiddenField;
                if (cbxSelect != null)
                {
                    if (cbxSelect.Checked)
                    {
                        entity = new WflCommentEntity();
                        Guid UserID = new Guid(item.GetDataKeyValue("UserID").ToString());

                        entity.InstanceID = TrackEntity.InstanceID;
                        entity.ActivityID = TrackEntity.ActivityID;
                        entity.TrackID = TrackEntity.TrackID;
                        entity.CommentAt = DateTime.MinValue;
                        entity.Comments = "";
                        entity.CommentStatus = (byte)Enumerator.ContractCommentStatus.Assign;
                        entity.CreatedAt = DateTime.Now;
                        entity.Deleted = false;
                        entity.LeaderID = UserID;
                        entity.LeaderName = txtUserName.Value;
                        Comments.Add(entity);
                    }
                }
            }

            if (Comments.Count <= 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "null", "alert('请选择用户!');", true);
                return;
            }

            foreach (WflCommentEntity comment in Comments)
            {
                bll.Insert(comment);
            }
            
            ClientScript.RegisterStartupScript(this.GetType(), "success", "window.close();", true);

        }
    }
}
